<?php

namespace App\Http\Controllers\Common;

use Illuminate\Http\Request;

use App\Service\WdhService;

use App\Http\Controllers\Controller;

class SmsController extends Controller
{

    /**
     * 发送短信
     * @param Request $request
     * @return array
     */
    public function sendSms( Request $request )
    {
        $args_data = [];

        $args_data['mobile'] = $request->input('mobile');
        $args_data['template'] = $request->input('template');
        $args_data['session_id'] = $request->input('session_id');
        $args_data['yzm'] = $request->input('yzm','');

        if (empty($args_data['mobile'] || empty($args_data['template']) || empty($args_data['session_id']))) {
            $body = $request->input('body');
            if ($body) {
                $body_arr = json_decode($body, true);
                $args_data['mobile'] = $body_arr['mobile'];
                $args_data['template'] = $body_arr['template'];
                $args_data['session_id'] = $body_arr['session_id'];
                $args_data['yzm'] = isset($body_arr['yzm']) ? $body_arr['yzm'] : '';
            }
        }

        if ( empty($args_data['mobile']) || !isMobile($args_data['mobile']) ) {
            return response()->json(['code' => 100001, 'message' => '手机号错误']);
        }

        if ( empty($args_data['template']) ) {
            return response()->json(['code' => 100002, 'message' => '短信模板不能为空']);
        }

        if ( empty($args_data['session_id']) ) {
            return response()->json(['code' => 100002, 'message' => 'session_id不能为空']);
        }

        return  WdhService::sendSms( $args_data );

    }


}
